package com.news.news.service;

import com.news.news.dao.NewsDetailDao;
import com.news.news.modle.*;
import com.news.news.util.JWTUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.System;
import java.util.List;


@Service
@Transactional
public class NewsDetailService {
    @Resource
    NewsDetailDao newsDetailDao;
    @Resource
    RedisTemplate redisTemplate;

    public News findNewsByid(Integer id) {
        return newsDetailDao.findNewsByid(id);
    }

    /**
     * 查询点赞、收藏数量
     * @param id
     * @return
     */
    public int[] findlikeCollectionNum(Integer id) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Integer dzNum= (Integer) valueOperations.get("news"+id+":"+"dzNum");//新闻id+:+dzNum
        Integer scNum= (Integer) valueOperations.get("news"+id+":"+"scNum");//新闻id+:+scNum
        Integer plNum= (Integer) valueOperations.get("news"+id+":"+"plNum");//新闻id+:+scNum
        if(dzNum==null){
            valueOperations.set("news"+id+":"+"dzNum",0);
        }
        if(scNum==null){
            valueOperations.set("news"+id+":"+"scNum",0);
        }
        if(plNum==null){
            valueOperations.set("news"+id+":"+"plNum",0);
        }

        dzNum= (Integer) valueOperations.get("news"+id+":"+"dzNum");//新闻id+user+用户id+:+dzNum
        scNum= (Integer) valueOperations.get("news"+id+":"+"scNum");//新闻id+user+用户id+:+scNum
        plNum= (Integer) valueOperations.get("news"+id+":"+"plNum");//新闻id+user+用户id+:+plNum
        return new int[]{dzNum,scNum,plNum};

    }

    public void updatelikeNum(Integer id, boolean dz, String newsName, String token) {
        Integer userid = JWTUtil.getTokenInfo(token).getClaim("id").asInt();
        //1.修改Redis数据库中本篇文章的点赞数量
        ValueOperations valueOperations = redisTemplate.opsForValue();
        if(dz){//点赞
            valueOperations.increment("news"+id+":"+"dzNum");//新闻id+:+dzNum
            valueOperations.set("news"+id+"user"+userid+"dz:", 1);//设置一个标识
            //2.将用户点赞相关文章信息存储在数据库中

        }else {
            valueOperations.decrement("news"+id+":"+"dzNum");//新闻id+:+dzNum
            redisTemplate.delete("news"+id+"user"+userid+"dz:");//设置一个标识
            //2.将用户点赞相关文章信息删除在数据库中

        }

    }

    public void updateCollectionNum(Integer id, boolean sc, String newsName, String token) {
        Integer userid = JWTUtil.getTokenInfo(token).getClaim("id").asInt();
        //1.修改Redis数据库中本篇文章的收藏数量
        ValueOperations valueOperations = redisTemplate.opsForValue();
        if(sc){//收藏
            valueOperations.increment("news"+id+":"+"scNum");//新闻id+:+dzNum
            valueOperations.set("news"+id+"user"+userid+"sc:", 1);//
            //2.将用户收藏相关文章信息存储在数据库中
            newsDetailDao.addCollect(id,newsName,userid);
        }else {
            valueOperations.decrement("news"+id+":"+"scNum");//新闻id+:+dzNum
            redisTemplate.delete("news"+id+"user"+userid+"sc:");//清空标识
            //2.将用户收藏相关文章信息删除在数据库中
            newsDetailDao.deleteCollect(userid,id);
        }

    }



    public int[] isUserDzSc(Integer id, String token) {
        Integer userid = JWTUtil.getTokenInfo(token).getClaim("id").asInt();
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Integer dzNum= (Integer) valueOperations.get("news"+id+"user"+userid+"dz:");//新闻id+:+dzNum是否点赞
        Integer scNum= (Integer) valueOperations.get("news"+id+"user"+userid+"sc:");//新闻id+:+scNum是否收藏
        Integer plNum= (Integer) valueOperations.get("news"+id+"user"+userid+"pl:");//新闻id+:+plNum是否p评论
//        System.out.println(dzNum + ";" + scNum);
        if(dzNum==null){//没有点赞
            dzNum=0;
        }
        if(scNum==null){//没有收藏
            scNum=0;
        }
        if(plNum==null){//没有收藏
            plNum=0;
        }
        return new int[]{dzNum,scNum,plNum};
    }

    /**
     * 发表评论
     * @param comment
     * @param token
     */
    public void commentSent(Comment comment, String token) {
        Integer userid = JWTUtil.getTokenInfo(token).getClaim("id").asInt();
        comment.setUserId(userid);
        newsDetailDao.commentSent(comment);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.increment("news"+comment.getNewsId()+":"+"plNum");//新闻id+:+dzNum
        valueOperations.set("news"+comment.getNewsId()+"user"+userid+"pl:", 1);//设置一个标识
    }

    public List<Comment> findComments(int id) {
        return newsDetailDao.findComments(id);
    }
}
